package org.jsola.hr.service.impl;

import org.jsola.contract.entity.TemplateVariableDO;
import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.INecessaryVariableDAO;
import org.jsola.hr.dto.NecessaryVariableAddDTO;
import org.jsola.hr.dto.NecessaryVariableUpdateDTO;
import org.jsola.hr.entity.AdditionalVariableDO;
import org.jsola.hr.entity.NecessaryVariableDO;
import org.jsola.hr.query.NecessaryVariableQuery;
import org.jsola.hr.service.INecessaryVariableService;
import org.jsola.hr.vo.NecessaryVariableListVO;
import org.jsola.hr.vo.NecessaryVariableVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 员工必填信息关联变量
 *
 * @author zhr
 */
@Service("hrNecessaryVariableServiceImpl")
public class NecessaryVariableServiceImpl implements INecessaryVariableService {

    @Autowired
    private INecessaryVariableDAO necessaryVariableDAO;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public NecessaryVariableVO save(NecessaryVariableAddDTO necessaryVariableAddDTO, TokenUser tokenUser) {
        // 转DO
        NecessaryVariableDO necessaryVariableDO = necessaryVariableAddDTO.to(NecessaryVariableDO.class);
        // 保存
        necessaryVariableDO = save(necessaryVariableDO, tokenUser.getSiteId(), tokenUser.getUserId());
        return necessaryVariableDO.to(NecessaryVariableVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public List<NecessaryVariableVO> batchSave(List<NecessaryVariableAddDTO> necessaryVariableAddDTOList, TokenUser tokenUser) {
        if(CollectionUtils.isEmpty(necessaryVariableAddDTOList)) {
            return null;
        }
        List<NecessaryVariableDO> necessaryVariableDOList = necessaryVariableAddDTOList
                .parallelStream()
                .map(necessaryVariableAddDTO -> {
                    NecessaryVariableDO necessaryVariableDO = necessaryVariableAddDTO.to(NecessaryVariableDO.class);
                    necessaryVariableDO.preInsert(tokenUser.getUserId());
                    necessaryVariableDO.setSiteId(tokenUser.getSiteId());
                    return necessaryVariableDO;
                }).collect(Collectors.toList());
        necessaryVariableDAO.insertListAndSetId(necessaryVariableDOList);
        return  necessaryVariableDOList
                .parallelStream()
                .map(necessaryVariableDO -> necessaryVariableDO.to(NecessaryVariableVO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(NecessaryVariableUpdateDTO necessaryVariableUpdateDTO, TokenUser tokenUser) {
        //转DO
        NecessaryVariableDO necessaryVariableDO = necessaryVariableUpdateDTO.to(NecessaryVariableDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(necessaryVariableDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, Long... necessaryVariableIds) {
        return deleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) necessaryVariableIds);
    }


    @Override
    public NecessaryVariableVO selectById(Long necessaryVariableId, String siteId) {
        NecessaryVariableDO necessaryVariableDO = selectDOById(necessaryVariableId, siteId);
        if (necessaryVariableDO == null) {
            return null;
        }
        return necessaryVariableDO.to(NecessaryVariableVO.class);
    }

    @Override
    public List<NecessaryVariableListVO> select(NecessaryVariableQuery necessaryVariableQuery, String siteId) {
        List<NecessaryVariableDO> necessaryVariableDOList = selectDO(necessaryVariableQuery, siteId);
        if (CollectionUtils.isEmpty(necessaryVariableDOList)) {
            return necessaryVariableDOList == null ? null : new ArrayList<>();
        }
        return necessaryVariableDOList.stream()
                .map(necessaryVariableDO -> necessaryVariableDO.to(NecessaryVariableListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(NecessaryVariableQuery necessaryVariableQuery, String siteId) {
        Example example = buildExample(necessaryVariableQuery, siteId);
        return necessaryVariableDAO.selectCountByExample(example);
    }

    @Override
    public Page<NecessaryVariableListVO> selectPage(NecessaryVariableQuery necessaryVariableQuery, String siteId) {
        Example example = buildExample(necessaryVariableQuery, siteId);
        Page<NecessaryVariableDO> page = necessaryVariableDAO.selectPageByExample(example,
                necessaryVariableQuery.getPageNo(),
                necessaryVariableQuery.getPageSize());

        return page.to(NecessaryVariableListVO.class);
    }

    @Override
    public NecessaryVariableDO selectDOById(Long necessaryVariableId, String siteId) {
        return listById(necessaryVariableId, siteId);
    }

    @Override
    public List<NecessaryVariableDO> selectDO(NecessaryVariableQuery necessaryVariableQuery, String siteId) {
        Example example = buildExample(necessaryVariableQuery, siteId);
        return necessaryVariableDAO.selectByExample(example);
    }

    @Override
    public List<NecessaryVariableDO> selectByTemplateId(Long templateId, String siteId) {

        Example example = new Example(NecessaryVariableDO.class);
        example.and()
                .andEqualTo("siteId", siteId)
                .andEqualTo("templateId", templateId);
        return necessaryVariableDAO.selectByExample(example);
    }

    @Override
    public List<NecessaryVariableDO> selectByTemplateIds(List<Long> templateIds, String siteId) {
        if (CollectionUtils.isEmpty(templateIds)) {
            return new ArrayList<>();
        }
        Example example = new Example(NecessaryVariableDO.class);
        example.and()
                .andEqualTo("siteId", siteId)
                .andIn("templateId", templateIds);
        return necessaryVariableDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param necessaryVariableQuery 查询参数
     * @param siteId                 所属站点id
     * @return example
     */
    private Example buildExample(NecessaryVariableQuery necessaryVariableQuery, String siteId) {
        Example example = new Example(NecessaryVariableDO.class);
        example.and()
                .andEqualTo("siteId", siteId);
        // 排序
        ExampleKit.setExampleOrder(example, necessaryVariableQuery.getOrders());
        return example;
    }
}




